#include "TonemapProcess.h"
#include "Utils/LogUtil.h"
#include "Render/RenderState.h"
#include "Game/GameState.h"
#include "Core/CoreGlobals.h"
#include "Game/Scene/Entity.h"
#include "Game/Component/Model.h"
#include "Render/Buffer/VertexBuffer.h"
#include "Render/Buffer/IndexBuffer.h"
#include "Render/Buffer/CustomBuffer.h"
#include "../LocalShader.h"
#include "../LocalScene.h"
#include "../ResourceProcess.h"
#include "../Shadows/ShadowsAtrousFilterProcess.h"
#include "../Reflections/ReflectionsAtrousFilterProcess.h"

void TonemapProcess::OnInit()
{
	mLocalScene = static_cast<LocalScene*>(mGameScene);
	mResourceProcess = mLocalScene->GetProcess<ResourceProcess>("ResourceProcess");
	mShadowsAtrousFilterProcess = mLocalScene->GetProcess<ShadowsAtrousFilterProcess>("ShadowsAtrousFilterProcess");
	mReflectionsAtrousFilterProcess = mLocalScene->GetProcess<ReflectionsAtrousFilterProcess>("ReflectionsAtrousFilterProcess");


	mPushConstants.exposure = 1.f;


	ENQUEUE_RENDER_COMMAND(BuildModel)([this](VkCommandBuffer commandBuffer)
		{
			RefCountPtr<RHISampler> trilinearSampler = GetSampler("ResourceProcess", "trilinearSampler");
			

			std::array<RefCountPtr<RHITexture>, 2> colorImage;

#if 1
			colorImage[0] = GetTexture("TaaProcess", "colorImage_s");
			colorImage[1] = GetTexture("TaaProcess", "colorImage_d");
#else
			colorImage[0] = GetTexture("GroundTruthPathRayTraceProcess", "colorImage_s");
			colorImage[1] = GetTexture("GroundTruthPathRayTraceProcess", "colorImage_d");
#endif


			mDescriptor = RHIDescriptor::Builder()
				.PushShader<TonemapVertexShader>()
				.PushShader<TonemapFragmentShader>()
				.SetDescriptorCount(0, 2)
				.Build();


			{
				mDescriptor->SetValue("colorImage", colorImage[0], trilinearSampler);
				mDescriptorMask[0] = mDescriptor->AllocateDescriptor();

				mDescriptor->SetValue("colorImage", colorImage[1], trilinearSampler);
				mDescriptorMask[1] = mDescriptor->AllocateDescriptor();
			}

			


			mColorRenderTarget = ColorRenderTarget::Builder()
				.SetFormat(VK_FORMAT_R16G16B16A16_SFLOAT)
				.SetUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)
				.SetStoreOp(VK_ATTACHMENT_STORE_OP_STORE)
				.SetFinalLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
				.Build(this, "colorRenderTarget");

			mPipeline = GraphicsPipeline::Builder()
				.SetDescriptor(mDescriptor)
				.AddColorRenderTarget(mColorRenderTarget)
				.SetDstStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)
				.SetDstAccessMask(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT)
				.SetSrcStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)
				.SetSrcAccessMask(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT)
				.Build();

			mRenderPass = RHIRenderPass::Builder()
				.AddRenderTarget(mColorRenderTarget)
				.AddPipeline(mPipeline)
				.SetSrcStageMask(VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT)
				.SetSrcAccessMask(VK_ACCESS_SHADER_READ_BIT)
				.SetDstStageMask(VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT)
				.SetDstAccessMask(VK_ACCESS_SHADER_READ_BIT)
				.Build();

			mFrameBuffer = RHIFrameBuffer::Builder()
				.SetRenderPass(mRenderPass)
				.Build();
		});
}

void TonemapProcess::OnTick(float deltaTime)
{
	if (GGameState->GetFrameAmount() < 2)
		return;

	ENQUEUE_RENDER_COMMAND(RecordCommand)([this](VkCommandBuffer commandBuffer)
		{

			VkRenderPassBeginInfo renderPassBeginInfo = mFrameBuffer->GetRenderPassBeginInfo();

			vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

			vkCmdSetViewport(commandBuffer, 0, 1, &GRenderState->GetViewport());
			vkCmdSetScissor(commandBuffer, 0, 1, &GRenderState->GetScissor());

			vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, mPipeline->GetPipeline());


			vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, mPipeline->GetPipelineLayout(), 0, 1, &mDescriptorMask[mResourceProcess->mCurrMask].GetDescriptorSet(), 0, nullptr);


			vkCmdPushConstants(commandBuffer, mPipeline->GetPipelineLayout(), VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushConstants), &mPushConstants);


			vkCmdDraw(commandBuffer, 3, 1, 0, 0);

			vkCmdEndRenderPass(commandBuffer);
		});
}

void TonemapProcess::OnResize()
{
	RefCountPtr<RHISampler> trilinearSampler = GetSampler("ResourceProcess", "trilinearSampler");

	std::array<RefCountPtr<RHITexture>, 2> colorImage;
	colorImage[0] = GetTexture("TaaProcess", "colorImage_s");
	colorImage[1] = GetTexture("TaaProcess", "colorImage_d");

	{
		mDescriptor->SetValue("colorImage", colorImage[0], trilinearSampler);
		mDescriptor->UpdateDescriptor(mDescriptorMask[0]);

		mDescriptor->SetValue("colorImage", colorImage[1], trilinearSampler);
		mDescriptor->UpdateDescriptor(mDescriptorMask[1]);
	}

	mColorRenderTarget = ColorRenderTarget::Builder()
		.SetFormat(VK_FORMAT_R16G16B16A16_SFLOAT)
		.SetUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)
		.SetStoreOp(VK_ATTACHMENT_STORE_OP_STORE)
		.SetFinalLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
		.Build(this, "colorRenderTarget");

	mFrameBuffer = RHIFrameBuffer::Builder()
		.AddRenderTarget(mColorRenderTarget)
		.SetRenderPass(mRenderPass)
		.Build();
}
